﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.IO;

namespace bulkuploader_gui
{
    /// <summary>
    /// Interaction logic for changeBackupLocation.xaml
    /// </summary>
    public partial class changeBackupLocation : Window
    {
        public changeBackupLocation()
        {
            InitializeComponent();
            XmlDocument doc = new XmlDocument();
            doc.Load("config.xml");
            if (doc.SelectSingleNode("//backupLocation") == null)
            {
                XmlElement location = doc.CreateElement("backupLocation");
                location.InnerText = "\\GAEbackups";
                doc.ChildNodes[1].AppendChild(location);
                doc.Save("config.xml");
            }
            locationBox.Text = doc.SelectSingleNode("//backupLocation").InnerText;
        }

        private void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            if(Directory.Exists(locationBox.Text)){
                XmlDocument doc = new XmlDocument();
                doc.Load("config.xml");
                
                if (doc.SelectSingleNode("//backupLocation") == null)
                {
                    XmlElement location = doc.CreateElement("backupLocation");
                    location.InnerText = "backup";
                    doc.ChildNodes[1].AppendChild(location);
                }
                else
                {
                    doc.SelectSingleNode("//backupLocation").InnerText = locationBox.Text;
                }
                doc.Save("config.xml");
                this.Close();
            }
            else
                System.Media.SystemSounds.Beep.Play();
        }

        private void changeBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();

            //show dialog box
            System.Windows.Forms.DialogResult result = dlg.ShowDialog();

            // OK button was pressed.
            if (result == System.Windows.Forms.DialogResult.OK  && Directory.Exists(dlg.SelectedPath))
            {
                locationBox.Text = dlg.SelectedPath;
            }
        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
